﻿using System;
using System.Threading;
using Skyline;

namespace QuantBox
{
    public interface IIdGenerator
    {
        long Next();
    }

    public class SyncTickIdGen : IIdGenerator
    {
        private long _last;

        private readonly object _locker = new();

        public SyncTickIdGen()
        {
            _last = DateTime.Now.Ticks;
        }
        public long Next()
        {
            lock (_locker)
            {
                var ticks = DateTime.Now.Ticks;
                while (_last == ticks)
                {
                    ticks = DateTime.Now.Ticks;
                }
                _last = ticks;
                return ticks;
            }
        }
    }

    public class TickIdGen : IIdGenerator
    {
        private long _last;
        public TickIdGen()
        {
            _last = DateTime.Now.Ticks;
        }
        public long Next()
        {
            var ticks = DateTime.Now.Ticks;
            while (_last == ticks)
            {
                ticks = DateTime.Now.Ticks;
            }
            _last = ticks;
            return ticks;
        }
    }

    public class IdGenerator : IIdGenerator
    {
        private long _value;

        public IdGenerator(int baseValue, int seriesValue)
        {
            _value = new MakeLong(baseValue, seriesValue).Value;
        }

        public long Next() => Interlocked.Increment(ref _value);
    }
}
